﻿Imports System.Reflection
Imports Runa.UI.Input
Imports Runa.Core.Services
Imports Runa.Foundation.Messaging

Partial Public Class GameRender

    ''' <summary>
    ''' Binding the InputBegin event to one event routine.
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub BindingInputDeviceEvent()
        Me.InputManager = New InputManager(Me.Game) With {.UpdateOrder = Integer.MinValue}

#If Not WINDOWS_PHONE Then
        Me.m_mouseInputHandler = New MouseInputHandler(Me.Game) With {.MouseSensitive = GamePackage.MouseSensitive}
#End If

        Me.m_keyboardInputHandler = New KeyboardInputHandler(Me.Game)

#If FEATURE_TOUCH Then
        Me.m_touchPadInputHandler = New TouchPadInputHandler(Me.Game)
#End If

        Me.InputManager.InputHandlers.Add(Me.m_mouseInputHandler)
        Me.InputManager.InputHandlers.Add(Me.m_keyboardInputHandler)

#If FEATURE_TOUCH Then
        Me.InputManager.InputHandlers.Add(Me.m_touchPadInputHandler)
#End If

        For Each handler In Me.InputManager.InputHandlers
            handler.Enabled = True
            AddHandler handler.DataArraived, AddressOf Me.OnInputDataArrived
#If NETFX_CORE Then
            For Each pInf In handler.GetType().GetRuntimeProperties()
                For Each attr In pInf.GetCustomAttributes()
#Else
            For Each pInf In handler.GetType().GetProperties()
                For Each attr In Attribute.GetCustomAttributes(pInf)
#End If
                    If TypeOf attr Is MessageMappingAttribute Then
                        Dim attrCustom = CType(attr, MessageMappingAttribute)
                        For Each item In attrCustom.MessageMappings
                            If Me.MessageHandlers.ContainsKey(item.Key) = False Then
                                Me.MessageHandlers.Add(item.Key, item.Value)
                            End If
                        Next
                    End If
                Next
            Next
        Next

        Me.Services.AddService(GetType(IEventsMappingService), Me)
        Me.Components.Add(Me.InputManager)
    End Sub

End Class